#include "Scroller.h"
#include "ScrollerItem.h"

#include <QGraphicsScene>
#include <QMouseEvent>
#include <QScrollBar>

const int ITEM_COUNT = 100;

Scroller::Scroller(QWidget *parent) :
    QGraphicsView(parent)
{
    setFrameStyle(QFrame::NoFrame);
    //setAttribute(Qt::WA_OpaquePaintEvent, true);
    setAttribute(Qt::WA_NoSystemBackground, true);

    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    QGraphicsScene* scene = new QGraphicsScene(this);
    scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    QBrush brush;
    brush.setStyle(Qt::SolidPattern);
    brush.setColor(Qt::black);
    setBackgroundBrush(brush);

    setScene(scene);

    for(int ii=0; ii<ITEM_COUNT; ++ii) {
        ScrollerItem* item = new ScrollerItem(ii);
        scene->addItem(item);
        item->setPos(0, (ii*80)+300);
    }

    m_scrollbar = verticalScrollBar();
    m_kinetic = new OLFlickAndDrag(*this, 0, 0, 300, this);


    connect(m_scrollbar, SIGNAL(rangeChanged(int, int)), m_kinetic, SLOT(rangeChange(int, int)));
}

void Scroller::mouseMoveEvent(QMouseEvent* event)
{
    m_kinetic->handleMouseMove(event->pos());
}

void Scroller::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton) {
        m_kinetic->handleMousePress(event->pos());
    }
}

void Scroller::mouseReleaseEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton) {
        m_kinetic->handleMouseRelease(event->pos());
    }
}

void Scroller::resizeEvent(QResizeEvent* event)
{
    setSceneRect(scene()->itemsBoundingRect().adjusted(0,-300, 0, 300));
}

int Scroller::scrollValue()
{
    return m_scrollbar->value();
}

void Scroller::setScrollValue(int scrollvalue)
{
    m_scrollbar->setValue(scrollvalue);
}

void Scroller::handleClick(const QPoint& pos)
{
}

void Scroller::handleLongPress(const QPoint& pos)
{
}

void Scroller::handleLeftSwipe()
{
}

void Scroller::handleRightSwipe()
{
}
